#include "ColorFactory.h"

ColorFactory* ColorFactory::instance = 0;

using namespace std;

ColorFactory::ColorFactory(){
}

void ColorFactory::destroy(){
	if(ColorFactory::instance!=NULL)
		delete ColorFactory::instance;
	ColorFactory::instance = NULL;
}

ColorFactory::~ColorFactory(){}


ColorFactory* ColorFactory::getInstance(){
	if(!ColorFactory::instance)
		ColorFactory::instance = new ColorFactory();
	return ColorFactory::instance;
}

Color* ColorFactory::getColor(std::string rgb) throw (InvalidColorException){
	
	if(rgb.size()!=9)throw InvalidColorException("No se pudo crear el color solicitado");
	if(!ValidatorUtils::isNumber(rgb))throw InvalidColorException("No se pudo crear el color solicitado");

	else{
		int red;
		int green;
		int blue;

		red = atoi(rgb.substr(0,3).c_str());
		green = atoi(rgb.substr(3,3).c_str());
		blue = atoi(rgb.substr(6,3).c_str());

		if(red>255||green>255||blue>255)
			throw InvalidColorException("No se pudo crear el color solicitado");
		else if(red<0||green<0||blue<0)
			throw InvalidColorException("No se pudo crear el color solicitado");
		else 
			return new Color(red,green,blue);

	}
}